package de.backtomedieval.activities.startscreen;

import de.backtomedieval.R;
import de.backtomedieval.activities.gamemenu.Gamemenu;
import de.backtomedieval.activities.gamemenu.ScenarioMenu;
import de.backtomedieval.main.Game;
import de.backtomedieval.scenarios.FreeGame;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class Startscreen extends Activity {
	private GridView grid_main;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.startscreen_main);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		Game.setLastActivity(this);

		grid_main = (GridView) findViewById(R.id.GridViewMenu);
		grid_main.setAdapter(new ImageAdapter(this));
		grid_main.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				onStartScreenMenuChoose(position);
			}
		});

	}

	public class ImageAdapter extends BaseAdapter {
		Context mContext;
		public static final int ACTIVITY_CREATE = 10;

		public ImageAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			// TODO Auto-generated method stub
			return 2;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View v;
			if (convertView == null) {
				LayoutInflater li = getLayoutInflater();
				v = li.inflate(R.layout.gridiconlarge, null);
				TextView tv = (TextView) v.findViewById(R.id.icon_text);
				ImageView iv = (ImageView) v.findViewById(R.id.icon_image);
				if (position == 0) {
					tv.setText(R.string.startscreen_buttonnewprofile);
					iv.setImageResource(R.drawable.icon);
				} else if (position == 1) {
					tv.setText(R.string.startscreen_buttonloadprofile);
					iv.setImageResource(R.drawable.icon);
				}
			}

			else {
				v = convertView;
			}
			return v;
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}
	}

	protected void onStartScreenMenuChoose(int position) {
		if (position == 0) {
			//Game.setStartScenario(new FreeGame());
			Intent newgameIntent = new Intent(Startscreen.this, NewProfile.class);
			startActivity(newgameIntent);
			this.finish();
		} else if (position == 1) {
			Game.showPopup("Coming soon...");
			/*
			Intent newscenarioIntent = new Intent(Startscreen.this,
					ScenarioMenu.class);
			startActivity(newscenarioIntent);
			this.finish();
			*/
		}
	}
}